package edu.fdzc.learn.controller;

import edu.fdzc.learn.common.Result;
import edu.fdzc.learn.entity.Comments;
import edu.fdzc.learn.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @PostMapping("{id}/add")
    public Result addComment(@PathVariable int id, @RequestBody Comments comment) {
        comment.setDiscussionId(id);
        commentService.insert(comment);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result deleteComment(@PathVariable int id) {
        Comments comments = commentService.findById(id);
        if(comments != null){
            commentService.delete(id);
            return Result.success();
        }
        return Result.error("评论不存在");
    }

    @GetMapping("/{id}")
    public Result<Comments> findById(@PathVariable int id) {
        Comments comments =  commentService.findById(id);
        return Result.success(comments);
    }

    @GetMapping("/discussion/{id}")
    public Result<List<Comments>> findAll(@PathVariable int id) {
        List<Comments> list = commentService.findAll(id);
        return Result.success(list);
    }


}
